package org.jef.socket.msg;

import lombok.extern.slf4j.Slf4j;
import org.jef.core.util.JsonUtils;

/**
 * @version 1.0
 * @description: websocket消息
 * @author: ChengZhi 0336
 * @create: 2024-08-23 11:11
 **/
@Slf4j
public class WebSocketMsg implements IMsg{

    private String msg;

    private Long userId;

    private String roomId;

    public WebSocketMsg(Object msg, String roomId) {
        this.msg = toJson(msg);
        this.roomId = roomId;
    }

    public WebSocketMsg(Object msg, Long userId, String roomId) {
        this(msg, roomId);
        this.userId = userId;
    }

    public WebSocketMsg(String msg, String roomId) {
        this.msg = msg;
        this.roomId = roomId;
    }

    public WebSocketMsg(String msg, Long userId, String roomId) {
        this(msg, roomId);
        this.userId = userId;
    }

    public WebSocketMsg(String msg) {
        this.msg = msg;
    }

    public WebSocketMsg() {

    }

    @Override
    public String getMsg() {
        return this.msg;
    }

    @Override
    public void setMsg(Object msg) {
        this.msg = toJson(msg);
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    @Override
    public Long getUserId() {
        return this.userId;
    }

    @Override
    public void setUserId(Long userId) {
        this.userId = userId;
    }

    @Override
    public String getRoomId() {
        return this.roomId;
    }

    @Override
    public void setRoomId(String roomId) {
        this.roomId = roomId;
    }

    private String toJson(Object obj) {
        try {
            return JsonUtils.toJson(obj);
        } catch (Exception e) {
            log.error("toJson error", e);
        }

        return "";
    }

}
